<template>
  <rx-dialog @handOk="handleSubmit" @cancel="cancel">
      <div style="padding: 10px;">
          <div>
              <a-button type="primary" @click="reloadPermission" v-if="saveData.id">
                  {{i18n('list.reload','重新加载')}}
              </a-button>
          </div>
        <div>
          <div class="tableTit">
              {{permissI18n('mainField','主表字段权限设置')}}
          </div>
          <form-permission-user-main ref="tabMain" v-if="showMain"
                                     :mainData="permissionData.main"></form-permission-user-main>
        </div>
        <div v-if="permissionData.buttons && permissionData.buttons.length>0">
          <div class="tableTit">
              {{permissI18n('btn','按钮权限设置')}}
          </div>
          <form-permission-user-tab-btn ref="tabBtn" v-if="showMain"
                                        :mainData="permissionData.buttons"></form-permission-user-tab-btn>
        </div>
        <div v-if="permissionData.tabList && permissionData.tabList.length>0">
          <div class="tableTit">
              {{permissI18n('tab','TAB权限设置')}}
          </div>
          <form-permission-user-tab-list ref="tabList" v-if="showMain"
                                         :mainData="permissionData.tabList"></form-permission-user-tab-list>
        </div>
        <div v-if="permissionData.sub && permissionData.sub.length>0">

          <template v-for="(dataObj, index) in permissionData.sub">

              <div class="tableTit">
                {{permissI18n('subTab','子表权限设置')}}
            </div>
            <form-sub-permission-edit ref="subTab" :mainData="dataObj"></form-sub-permission-edit>
          </template>
        </div>

        <div v-if="permissionData.subtableRights && permissionData.subtableRights.length>0">
          <div class="tableTit">
              {{permissI18n('subTab','子表权限设置')}}
          </div>
          <form-sub-alias-permission-edit v-if="showMain" ref="subTabAlias"
                                          :attrList="mainAttrList"
                                          :subtableRights="permissionData.subtableRights">

          </form-sub-alias-permission-edit>
        </div>
      </div>


  </rx-dialog>
</template>
<script>
import FormPermissionApi from '@/api/form/core/formPermission'
import {BaseForm, RxDialog, RxLayout} from "@lowcode/jpaas-common-lib";
import FormPermissionUserMain from './formPermission/FormPermissionUserMain';
import FormPermissionUserTabBtn from './formPermission/FormPermissionUserTabBtn';
import FormPermissionUserTabList from "./formPermission/FormPermissionUserTabList";
import FormSubPermissionEdit from './formPermission/FormSubPermissionEdit';
import FormSubAliasPermissionEdit from './FormSubAliasPermissionEdit';
import FormBoDefApi from "@/api/form/bo/formBoDef";


export default {
    name: 'FormPermissionEdit',
    mixins: [BaseForm],
    components: {
        FormPermissionUserTabBtn,
        FormPermissionUserTabList,
        RxDialog,
        FormPermissionUserMain,
        FormSubAliasPermissionEdit,
        FormSubPermissionEdit,
        RxLayout
    },
    props: {
        /**
         * 权限类型
         */
        saveType: {
            type: String,
            default: ""
        },
        /**
         * 表单方案ID
         */
        pkId: {
            type: String,
            default: ""
        },
        /**
         * 业务模型ID
         */
        boDefId:{
            type: String,
            default: ""
        }

    },
    data() {
        return {
            permissionData: {},
            showMain: false,
            saveData: {
                //权限ID
                id:""
            },
            //主表字段。
            mainAttrList:[]
        }
    },
    created(){
        let self_=this;
        FormBoDefApi.getBoDef(this.boDefId).then(res=>{
            self_.mainAttrList=res.data.formBoEntity.boAttrList;
        })
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"formPermiss",key);
        },
        permissI18n(name,text,key){
            return this.$ti18(name,text,"formPermiss.list",key)+" "+this.$ti18("permissSet","权限设置","formPermiss.list",key);
        },
        get(id) {
            var self_ = this;
            this.saveData.configId = id;
            return FormPermissionApi.get(this.saveType, id).then(res => {
                var data = res.data;
                self_.permissionData = data.permission;
                self_.saveData.id = data.id;
                self_.showMain = true;
            })
        },
        save(values) {

            var saveData = {
                "main": this.$refs.tabMain.getMainData(),
                "buttons": {},
                "tabList": {},
                "sub": {}
            };



            //按钮权限
            if (this.permissionData.buttons && this.permissionData.buttons.length > 0) {
                saveData.buttons = this.$refs.tabBtn.getMainData();
            }
            //TAB权限
            if (this.permissionData.tabList && this.permissionData.tabList.length > 0) {
                saveData.tabList = this.$refs.tabList.getMainData();
            }
            var subList = this.$refs.subTab;
            for (var i = 0; subList && i < subList.length; i++) {
                var subData = subList[i].getMainData();
                saveData.sub[subData.alias] = subData.dataObj;
            }
            var subTabAlias = this.$refs.subTabAlias;
            if (subTabAlias) {
                saveData.subtableRights = this.permissionData.subtableRights;
            }

            if (this.saveData.id) {
                values.pkId = this.saveData.id;
            } else {
                values.pkId = null
            }
            values.configId = this.saveData.configId;
            values.permission = JSON.stringify(saveData);
            values.type = this.saveType;
            //新增时保存应用ID
            let appId = this.curApp.appId;
            if (appId) {
                values.appId = appId;
            }


            return FormPermissionApi.save(values);
        },
        //重新加载表单方案权限
        reloadPermission() {
            let self_=this;
            this.$confirm({
                title: this.i18n('form.tipInfo','提示信息'),
                content: this.i18n('form.tipContent','重新加载'),
                okText:this.$ti18('rxForm.ackBtn','确认'),
                cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                zIndex:30020,
                onOk(){
                    FormPermissionApi.reloadFormPermission(self_.pkId).then(res => {
                        if (res.success) {
                            self_.saveData.id="";
                            self_.permissionData= res.data;
                        }
                    });
                }
            });

        }
    }
}
</script>
<style scoped>
.tableTit {
    margin: 10px 10px 10px 0px;
    border-left: 2px solid #1890ff;
    padding-left: 6px;
    height: 14px;
    line-height: 14px;
}

* >>> .ant-table-row td:last-child .MyfixedBoxs {
    right: 10px;
}
</style>